var Qlsolution = {

	/*
	 * autor info
	 */
	author: 'Adam Zakrzewski',
	www: ['http://www.qlsolution.com', 'http://www.qlpage.com'],
	
	/*
	 * helpers
	 */
	helpers: {
		isArray: function(input){
			return (typeof(input)=='object' && (input instanceof Array));
		}
	},

	/*
	 * global settings
	 */
	globalSettings: {
		defaultTemplate: '#mailings',
		checkUrlIntervalTime: 250,
		checkHeightIntervalTime: 250,
		serverTimeout: 5
	},
	
	/*
	 * user data
	 */
	userData: new Object(),
	
	/*
	 * layout settings
	 */
	setLayout: function(settings){
		var footerSelector = $('footer'),
			htmlSelector = $('html'),
			headerSelector = $('header'),
			headerAnimate = false;
		setInterval(function(){
			//set footer
			if(parseInt(htmlSelector.height(), 10) > parseInt($(window).height(), 10)){
				footerSelector.css('position', 'relative');
			}
			else{
				footerSelector.css('position', 'absolute');
			}
			//set header opacity
			if((parseInt($(document).scrollTop(), 10) > 0) && (!headerAnimate)){
				headerAnimate = true;
				headerSelector.animate({
					opacity: 0.8
				}, function(){
					headerAnimate = false;
				})
			}
			if((parseInt($(document).scrollTop(), 10) == 0) && (!headerAnimate)){
				headerAnimate = true;
				headerSelector.animate({
					opacity: 1
				}, function(){
					headerAnimate = false;
				})
			}
		}, Qlsolution.globalSettings.checkHeightIntervalTime)
	},

	/*
	 * initialize menu
	 */
	menu:[],

	/*
	 * initialize widgets
	 */
	widgets:[],

	/*
	 * initialize controllers
	 */
	controllers:[],

	/*
	 * set main navigation
	 */
	setNav: function(){
		//create nav
		$('header.main nav').append($('<ul/>'));
		for(val in Qlsolution.menu){
			$('header.main nav ul').append('<li class="' + Qlsolution.menu[val].cls  + ' cp"><a href="' + Qlsolution.menu[val].href + '">' + Qlsolution.menu[val].text + '</a></li>');
		}
	},

	/*
	 * load default template, check URL
	 */
	checkUrl: function(){
		//set default template
		if (window.location.hash == '') {
			window.location.hash = Qlsolution.globalSettings.defaultTemplate;
		}
		var lastView = '',
			hashUrl;
		var checkUrlInterval = setInterval(function(){
			hashUrl = window.location.hash;
			hashUrl = hashUrl.split(',');
			if(hashUrl[0] != lastView){
				lastView = hashUrl[0];
				$('section.main .content').html('');
				$('section.main #loading').show();
				$('section.main .content').load('index.php/' + lastView.substring(1), function(){
					$('section.main #loading').hide();
				});
				//set nav
				$('header.main nav ul li').removeClass('active');
				$('header.main nav ul li a[href="#' + lastView.substring(1) + '"]').parent().addClass('active');
			}
		}, Qlsolution.globalSettings.checkUrlIntervalTime);
	},
	
	/*
	 * alerts hide
	 */
	alerts: {
		hideAlert: function(){
			$('.alert').live('click', function(){	
				$(this).slideUp('normal', function(){$(this).remove()});
			})
		},
		showAlert: function(settings){
			if(settings.selector.find('.alert.' + settings.type).length > 0){
				settings.selector.find('.alert.' + settings.type).trigger('click');
			}
			settings.selector.prepend('<div class="alert ' + settings.type + '">' + settings.content + '</div>');
			settings.selector.find('.alert.' + settings.type).slideDown();
		},
		parseMsg: function(settings){
			try{
				settings.msg = $.parseJSON(settings.msg);
				if(
					settings.msg.code == 200){Qlsolution.alerts.showAlert({selector:settings.selector, type:'success', content:settings.success});
					return true;
				}
				else{
					Qlsolution.alerts.showAlert({selector:settings.selector, type:'warning', content:settings.warning});
				}
			}
			catch(err){
				Qlsolution.alerts.showAlert({selector:settings.selector, type:'warning', content:settings.warning});
			}
		}
	},
	
	/*
	 * tools
	 */
	tools: {
		disable: function(settings){
			if(Qlsolution.helpers.isArray(settings.selector)){
				var arrayLength = settings.selector.length;
				for (var i=0; i<arrayLength; i++){
					if(settings.selector[i].find('.tool.disable').length == 0){
						settings.selector[i].prepend('<div class="tool disable" />');
					}
				}
			}
			else{
				if(settings.selector.find('.tool.disable').length == 0){
					settings.selector.prepend('<div class="tool disable" />');
				}
			}
		},
		disableRemove: function(settings){
			if(Qlsolution.helpers.isArray(settings.selector)){
				var arrayLength = settings.selector.length;
				for (var i=0; i<arrayLength; i++){
					settings.selector[i].find('.tool.disable').fadeOut('normal', function(){$(this).remove()});
				}
			}
			else{
				settings.selector.find('.tool.disable').fadeOut('normal', function(){$(this).remove()});
			}
		},
		progressBar: function(settings){
			var percent = '0%';
			if((typeof(settings.count) != 'undefined') && (typeof(settings.total) != 'undefined')){
				percent = Math.round((100 * parseInt(settings.count, 10)) / parseInt(settings.total, 10)) + '%';
			}
			if(settings.selector.find('.tool.progressBar').length > 0){
				settings.selector.find('.tool.progressBar p').html(percent)
				settings.selector.find('.tool.progressBar .progressBarProgressVal').css('width', percent);
			}
			else{
				settings.selector.prepend('<div class="tool progressBar"><div class="tool progressBarTotalBg"></div><div class="progressBarProgressBg"><div class="progressBarProgressVal" style="width:' + percent + '"><p>' + percent + '</p></div></div></div>');
			}
		},
		progressBarRemove: function(settings){
			settings.selector.find('.tool.progressBar').fadeOut('normal', function(){$(this).remove()});
		}
	}
};